/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.control.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.control.dto.RiskMeasureTaskRecordExcelDTO;
import com.hopes.bizz.risk.control.entity.RiskMeasureTaskRecordEntity;
import com.hopes.bizz.risk.control.service.RiskMeasureTaskRecordService;
import com.hopes.bizz.risk.control.vo.RiskMeasureTaskRecordExcelVO;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 隐患排查任务
 *
 * @author cx
 * @date 2023-07-21 10:53:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskPointCheckTask" )
@Tag(description = "RiskPointCheckTask" , name = "隐患排查任务管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskMeasureTaskRecordController {

    private final RiskMeasureTaskRecordService riskMeasureTaskRecordService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskMeasureTaskRecord 隐患排查任务
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_view')" )
    public R getRiskMeasureTaskRecordPage(Page page, RiskMeasureTaskRecordEntity riskMeasureTaskRecord) {
        LambdaQueryWrapper<RiskMeasureTaskRecordEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(ObjectUtil.isNotNull(riskMeasureTaskRecord.getTaskName()),RiskMeasureTaskRecordEntity::getTaskName,riskMeasureTaskRecord.getTaskName());
        wrapper.like(ObjectUtil.isNotNull(riskMeasureTaskRecord.getCheckContent()),RiskMeasureTaskRecordEntity::getCheckContent,riskMeasureTaskRecord.getCheckContent());
        wrapper.like(ObjectUtil.isNotNull(riskMeasureTaskRecord.getResponsibleUserIds()),RiskMeasureTaskRecordEntity::getResponsibleUserIds,riskMeasureTaskRecord.getResponsibleUserIds());
        wrapper.ge(ObjectUtil.isNotNull(riskMeasureTaskRecord.getTaskStartTime()),RiskMeasureTaskRecordEntity::getTaskStartTime,riskMeasureTaskRecord.getTaskStartTime());
        wrapper.le(ObjectUtil.isNotNull(riskMeasureTaskRecord.getTaskDeadlineTime()),RiskMeasureTaskRecordEntity::getTaskDeadlineTime,riskMeasureTaskRecord.getTaskDeadlineTime());
        wrapper.eq(ObjectUtil.isNotNull(riskMeasureTaskRecord.getRiskControlId()), RiskMeasureTaskRecordEntity::getRiskControlId, riskMeasureTaskRecord.getRiskControlId());
        wrapper.eq(ObjectUtil.isNotNull(riskMeasureTaskRecord.getTaskStatus()),RiskMeasureTaskRecordEntity::getTaskStatus,riskMeasureTaskRecord.getTaskStatus());
		wrapper.eq(ObjectUtil.isNotNull(riskMeasureTaskRecord.getWorkType()),RiskMeasureTaskRecordEntity::getWorkType,riskMeasureTaskRecord.getWorkType());
		wrapper.eq(ObjectUtil.isNotNull(riskMeasureTaskRecord.getTaskHandleUserId()),RiskMeasureTaskRecordEntity::getTaskHandleUserId,riskMeasureTaskRecord.getTaskHandleUserId());
		wrapper.eq(ObjectUtil.isNotNull(riskMeasureTaskRecord.getCheckStatus()),RiskMeasureTaskRecordEntity::getCheckStatus,riskMeasureTaskRecord.getCheckStatus());
		wrapper.eq(ObjectUtil.isNotNull(riskMeasureTaskRecord.getRiskCheckConfigId()), RiskMeasureTaskRecordEntity::getRiskCheckConfigId, riskMeasureTaskRecord.getRiskCheckConfigId());
		wrapper.orderByAsc(RiskMeasureTaskRecordEntity::getTaskStatus).orderByDesc(RiskMeasureTaskRecordEntity::getTaskStartTime);
        Page page1 = riskMeasureTaskRecordService.page(page, wrapper);
        return R.ok(page1);
    }


    /**
     * 通过id查询隐患排查任务
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(riskMeasureTaskRecordService.getById(id));
    }

    /**
     * 新增隐患排查任务
     * @param riskMeasureTaskRecord 隐患排查任务
     * @return R
     */
    @Operation(summary = "新增隐患排查任务" , description = "新增隐患排查任务" )
    @SysLog("新增隐患排查任务" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_add')" )
    public R save(@RequestBody RiskMeasureTaskRecordEntity riskMeasureTaskRecord) {
        return R.ok(riskMeasureTaskRecordService.save(riskMeasureTaskRecord));
    }

    /**
     * 修改隐患排查任务
     * @param riskMeasureTaskRecord 隐患排查任务
     * @return R
     */
    @Operation(summary = "修改隐患排查任务" , description = "修改隐患排查任务" )
    @SysLog("修改隐患排查任务" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_edit')" )
    public R updateById(@RequestBody RiskMeasureTaskRecordEntity riskMeasureTaskRecord) {
        return R.ok(riskMeasureTaskRecordService.updateById(riskMeasureTaskRecord));
    }

    /**
     * 通过id删除隐患排查任务
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除隐患排查任务" , description = "通过id删除隐患排查任务" )
    @SysLog("通过id删除隐患排查任务" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskMeasureTaskRecordService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_export')")
    public List<RiskMeasureTaskRecordExcelVO> template() {
        return riskMeasureTaskRecordService.exportExcel(Arrays.asList(new RiskMeasureTaskRecordEntity()));
    }

    /**
     * 导出excel 表格
     * @param riskMeasureTaskRecordExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_export')")
    public List<RiskMeasureTaskRecordExcelVO> export(RiskMeasureTaskRecordExcelDTO riskMeasureTaskRecordExcelDto) {
        return riskMeasureTaskRecordService.exportExcel(riskMeasureTaskRecordService.list());
    }

    /**
     * 导入excel 表格
     * @param riskMeasureTaskRecordExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('control_risk_point_check_task_export')")
    public R importExcel(@RequestExcel List<RiskMeasureTaskRecordExcelDTO> riskMeasureTaskRecordExcelDtoList, BindingResult bindingResult) {
        return riskMeasureTaskRecordService.importExcel(riskMeasureTaskRecordExcelDtoList, bindingResult);
    }
}
